package com.corpgovernment.organization.mq;

import com.corpgovernment.common.mq.enums.MessageBizTypeEnum;
import com.corpgovernment.common.mq.producer.CommonMQProducer;
import com.ctrip.corp.obt.generic.utils.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class MultiTenantBudgetNotifyProducer {

    @Autowired
    private CommonMQProducer commonMQProducer;

    /**
     * 发送预算阀值告警消息
     */
    public void sendBudgetNotifyMsg(String budgetId) {
        try {
            Message message = new Message();
            message.setTopic(MessageBizTypeEnum.BUDGET_NOTIFY.getTopic());
            message.setBody(JsonUtils.toJsonString(budgetId).getBytes());
            message.setTags(MessageBizTypeEnum.BUDGET_NOTIFY.getTags());
            //延迟投递，确保事务提交
            message.setDelayTimeLevel(2);
            commonMQProducer.send(message);
        } catch (Exception e){
            log.error("发送预算阀值告警消息:{}", e);
        }
    }
}
